package xcmg.device.web.warehouse;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.warehouse.BillTypeConfigVO;
import xcmg.device.service.warehouse.BillTypeConfigService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

@RestController
@RequestMapping("/billTypeConfig")
public class BillTypeConfigController extends BasicController {
    @Autowired
    private BillTypeConfigService billTypeConfigService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加单据配置类型记录")
    @FastMappingInfo(needLogin = true,code = AuthCode.BillType.ADD)
    public ActionResult warehouse(@RequestBody BillTypeConfigVO billTypeConfigVO)throws Exception{
        if(null == billTypeConfigVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode = billTypeConfigService.add(billTypeConfigVO, getUserId(), getOrgId());
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新单据配置类型")
    @FastMappingInfo(needLogin = true,code = AuthCode.BillType.EDIT)
    public ActionResult update(@RequestBody BillTypeConfigVO billTypeConfigVO)throws Exception{
        if(null == billTypeConfigVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode = billTypeConfigService.update(billTypeConfigVO);
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ApiOperation("单据配置类型记录删除")
    @FastMappingInfo(needLogin = true,code = AuthCode.BillType.DEL)
    public ActionResult remove(@RequestBody String[] ids){
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(billTypeConfigService.remove(Arrays.asList(ids)));
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult warehouse(String id)throws Exception{
        return actionValueResult(billTypeConfigService.findByPK(id));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("查询单据配置类型记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<BillTypeConfigVO> condition)throws Exception{
        BillTypeConfigVO billTypeConfigVO =condition.getFilters();
        if(billTypeConfigVO == null){
            billTypeConfigVO = new BillTypeConfigVO();
        }
        billTypeConfigVO.setOrgId(getOrgId());
        condition.setFilters(billTypeConfigVO);
        PageCommonVO pageCommonVO= billTypeConfigService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }
    
    @RequestMapping(value = "/listAllOutTypes",method = RequestMethod.POST)
    @ApiOperation("查询单据配置类型记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult listAllOutTypes() throws Exception {
    	BillTypeConfigVO billTypeConfigVO = new BillTypeConfigVO();
    	billTypeConfigVO.setInOut(1);
    	billTypeConfigVO.setOrgId(getOrgId());
    	List<BillTypeConfigVO> billTypeConfigVOs = billTypeConfigService.listAllOutTypes(billTypeConfigVO);
    	return actionValueResult(billTypeConfigVOs);
    }
}
